﻿using System.Linq.Expressions;
using K9Nano.Extensions;
using K9Nano.Logging;

namespace K9Nano.Application.Logging;

public class ApiLogPagedInput
{
    public int PageIndex { get; set; } = 1;

    public int PageSize { get; set; } = 10;

    public int Skip() => (PageIndex - 1) * PageSize;
    public string? Controller { get; set; }
    public string? Action { get; set; }
    public int? StatusCode { get; set; }
    public DateTime From { get; set; }
    public DateTime To { get; set; }

    public virtual Expression<Func<ApiLogEntity, bool>>? GetPredicate()
    {
        Expression<Func<ApiLogEntity, bool>> filter = a => a.CreatedAt >= From && a.CreatedAt <= To;

        return filter
            .AndAlsoIf(!string.IsNullOrEmpty(Controller), a => a.ControllerName.Contains(Controller!))
            .AndAlsoIf(!string.IsNullOrEmpty(Action), a => a.ActionName.Contains(Action!))
            .AndAlsoIf(StatusCode != null, a => a.StatusCode == StatusCode);
    }

    public virtual IQueryable<ApiLogEntity> Sort(IQueryable<ApiLogEntity> query)
    {
        return query.OrderByDescending(a => a.Id);
    }
}
